﻿namespace WMS6.Web.Billing
{
    using System;
    using System.Collections.Generic;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.BaseInfoServiceRef;

    public class Tariff : BasePage
    {
        protected Button btnCopy;
        protected Button btnDelete;
        protected Button btnGoBack;
        protected Button btnNew;
        protected Button btnSave;
        protected CheckBox chkIsActive;
        protected DropDownList ddlTariffType;
        protected HtmlForm F9041;
        protected HtmlInputHidden hdnFlag;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnTariffID;
        protected HtmlHead Head1;
        protected HtmlImage img1;
        protected Label lblDesrc;
        protected Label lblMsg;
        protected Label lblOwner;
        protected Label lblTariffID;
        protected Label lblTariffType;
        protected Label lblTypeCode;
        protected HtmlLink mycss;
        protected Label one1;
        protected ScriptManager ScriptManager1;
        protected TextBox txtDescr;
        protected TextBox txtTariffID;
        protected TextBox txtTypeCode;
        protected UpdatePanel up1;

        protected void btnCopy_Click(object sender, EventArgs e)
        {
            this.txtTariffID.Text = "";
            this.txtTariffID.Enabled = true;
            base.DataEntityViewState = null;
            this.hdnFlag.Value = "";
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.txtTariffID.Text.Trim()))
            {
                try
                {
                    List<string> TariffIDS = new List<string> {
                        this.txtTariffID.Text.Trim()
                    };
                    this.lblMsg.Text = BaseInfoServiceClientProxy.GetInstance().Delete(TariffIDS, "TARIFF") + base.GetGlobalMsgResource("F9140_007");
                    base.DataEntityViewState = null;
                    this.btnCopy.Enabled = false;
                    this.btnDelete.Enabled = false;
                    this.btnSave.Enabled = false;
                }
                catch (Exception ex)
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F9140_008") + ex.Message;
                }
            }
        }

        protected void btnGoBack_Click(object sender, EventArgs e)
        {
            switch (this.hdnFrom.Value)
            {
                case "query":
                    base.Response.Redirect("TariffQuery.aspx", false);
                    break;

                case "list":
                    base.Response.Redirect("TariffList.aspx?from=edit", false);
                    break;
            }
        }

        protected void btnNew_Click(object sender, EventArgs e)
        {
            base.Server.Transfer(string.Format("Tariff.aspx?from={0}", base.UrlEncode(this.hdnFrom.Value.Trim())), false);
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtTypeCode.Text.Trim()))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F9140_002");
                this.txtTypeCode.Focus();
            }
            else if (this.hdnFlag.Value == "1")
            {
                this.UpdateTariffInfo();
            }
            else
            {
                this.InsertTariffInfo();
            }
        }

        protected void ddlTariffType_TextChanged(object sender, EventArgs e)
        {
            if (this.ddlTariffType.SelectedValue.ToUpper() == "FIXED")
            {
                this.img1.Attributes.Add("onclick", "selectRate();");
            }
            else
            {
                this.img1.Attributes.Add("onclick", "selectcalendarGroup();");
            }
            this.txtTypeCode.Text = "";
        }

        private void InsertTariffInfo()
        {
            try
            {
                TariffInfo tariffInfo = new TariffInfo {
                    TariffID = this.txtTariffID.Text,
                    Descr = this.txtDescr.Text,
                    TariffType = this.ddlTariffType.SelectedValue,
                    TypeCode = this.txtTypeCode.Text,
                    IsActive = this.chkIsActive.Checked ? "Y" : "N",
                    CreatedBy = base.UserInfo.UserID
                };
                string strTariffID = string.Empty;
                strTariffID = BaseInfoServiceClientProxy.GetInstance().Insert(tariffInfo, "TARIFF");
                this.hdnFlag.Value = "1";
                base.DataEntityViewState = tariffInfo;
                this.txtTariffID.Text = strTariffID;
                this.txtTariffID.Enabled = false;
                this.lblMsg.Text = strTariffID + base.GetGlobalMsgResource("F9140_005");
            }
            catch (Exception)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F9140_006") + "该价目编号：" + this.txtTariffID.Text.Trim() + "已存在";
            }
        }

        private void LoadData()
        {
            base.BindData2DDLBySysCode(this.ddlTariffType, "TariffType");
            if (!string.IsNullOrEmpty(base.Request.QueryString["ID"]))
            {
                this.hdnFlag.Value = "1";
                this.hdnTariffID.Value = base.Request.QueryString["ID"];
                this.SetTariffInfo2Form();
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetTariffInfo2Form()
        {
            try
            {
                TariffInfo tariffInfo = (TariffInfo) BaseInfoServiceClientProxy.GetInstance().GetInfoByID(this.hdnTariffID.Value, "TARIFF");
                if (tariffInfo != null)
                {
                    this.txtTariffID.Text = tariffInfo.TariffID;
                    this.txtTariffID.Enabled = false;
                    this.txtDescr.Text = tariffInfo.Descr;
                    this.ddlTariffType.SelectedValue = tariffInfo.TariffType;
                    this.ddlTariffType_TextChanged(null, null);
                    this.txtTypeCode.Text = tariffInfo.TypeCode;
                    this.chkIsActive.Checked = tariffInfo.IsActive == "Y";
                    base.DataEntityViewState = tariffInfo;
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void UpdateTariffInfo()
        {
            try
            {
                TariffInfo tariffInfo = base.DataEntityViewState as TariffInfo;
                tariffInfo.Descr = this.txtDescr.Text;
                tariffInfo.TariffType = this.ddlTariffType.SelectedValue;
                tariffInfo.TypeCode = this.txtTypeCode.Text;
                tariffInfo.IsActive = this.chkIsActive.Checked ? "Y" : "N";
                tariffInfo.UpdatedBy = base.UserInfo.UserID;
                this.lblMsg.Text = BaseInfoServiceClientProxy.GetInstance().Update(tariffInfo, "TARIFF") + base.GetGlobalMsgResource("F9140_003");
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F9140_004") + ex.Message;
            }
        }
    }
}

